package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "公告管理接口")
@RestController
@RequestMapping("/api/notices")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Operation(summary = "更新公告信息")
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice){
        //调用NoticeService中的update方法
        boolean update= noticeService.updateById(notice);
        if(update){
            return Result.success("更新成功");
        }else{
            return Result.error("更新失败");
        }

    }
    @Operation(summary = "切换公告状态")
    @PutMapping("/toggle/{id}")
    public Result toggle(@PathVariable Long id, @RequestParam Boolean isActive){
        //创建LambdaUpdateWrapper对象
        LambdaUpdateWrapper<Notice> noticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //设置要更新的字段
        noticeLambdaUpdateWrapper.set(Notice::getIsActive,isActive).set(Notice::getUpdateTime,new Date());
        //设置查询条件
        noticeLambdaUpdateWrapper.eq(Notice::getId,id);
        //调用NoticeService中的update方法
        boolean update=noticeService.update(noticeLambdaUpdateWrapper);
        if(update){
            return Result.success("切换成功");
        }else {
            return Result.error("切换失败");
        }

    }
    @Operation(summary = "发布新公告")
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice){
        //调用NoticeService中的save方法
        boolean save=noticeService.save(notice);
        if(save){
            return Result.success("发布成功");
        }else {
            return Result.error("发布失败");
        }
    }
    @Operation(summary = "根据id获取公告")
    @GetMapping("/{id}")
    public Result<Notice> getById(@PathVariable Long id){
        //调用NoticeService中的getById方法
        Notice notice=noticeService.getById(id);
        return Result.success(notice);
    }
    @Operation(summary = "获取所有公告")
    @GetMapping("/list")
    public Result<List<Notice>> list(){
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper=new LambdaQueryWrapper<>();
       //按照排序字段升序排序
        noticeLambdaQueryWrapper.orderByAsc(Notice::getPriority);
        //调用NoticeService中的list方法
        List<Notice> list= noticeService.list(noticeLambdaQueryWrapper);
        return Result.success(list);
    }
    @Operation(summary = "获取最新的公告")
    @GetMapping("/latest")
    public Result<Notice> latest() {
        Notice notice = noticeService.getOne(
                new LambdaQueryWrapper<Notice>()
                        .eq(Notice::getIsActive, true)
                        .orderByDesc(Notice::getPriority)   // 优先级高的在前
                        .orderByDesc(Notice::getCreateTime) // 再按时间倒序
                        .last("LIMIT 1"),                   // 只取一条
                false                                     // 不抛异常，找不到返回 null
        );
        return Result.success(notice);
    }
    @Operation(summary = "获取启用的公告")
    @GetMapping("/active")
    public Result acticeList(){
        //调用NoticeService中的list方法
        List<Notice> list = noticeService.list(new LambdaQueryWrapper<Notice>().eq(Notice::getIsActive, true));
        return Result.success(list);

    }
    @Operation(summary = "删除公告")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        //调用NoticeService中的removeById方法
        boolean delete=noticeService.removeById(id);
        if(delete){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    }


